<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<link rel="stylesheet" type="text/css" href="css/main.css">
<title>查看用户</title>
<style type="text/css">
body {
	background-image: url(./images/homelogo.png);
	background-color: #004350;
}
/*表格样式*/
.tbData {
	border-collapse: collapse;
	border-spacing: 0px;
	border: solid 3px #ddd;
	font-size: 14px;
	font-family: Consolas;
	color: #333;
	background-color: white;
	margin: 10px 0px;
}

.tbData th,td {
	border: solid 1px #ddd;
	padding: 5px 8px;
}

.tbData th {
	border-bottom: solid 2px #ddd;
	background-color: #eef;
	font-weight: normal;
	color: blue;
}

#divData {
	margin-left: 400px;
	margin-top: 200px;
}

#divData .tbData tr:hover {
	background-color: #fef;
}

/*分页样式*/
#divPage {
	text-align: left;
	margin: 10px 0px;
	margin-left: 400px;
	height: 30px;
	font-size: 12px;
}

#divPage a,#divPage span {
	text-decoration: none;
	color: Blue;
	background-color: White;
	padding: 3px 5px;
	font-family: Consolas;
	text-align: center;
	border: solid 1px #ddd;
	display: inline-block;
}

#divPage span {
	color: gray;
}

#divPage a:hover {
	color: Red;
}

#divPage .aCur {
	background-color: green;
	color: White;
	font-weight: bold;
}
</style>
<script type="text/javascript" src="js/jquery.js"></script>
<script type="text/javascript" src="js/jquery.query.js"></script>
<script type="text/javascript">
	//js表格 生成表格代码
	//arrTh 表头信息
	//arrTr 数据
	var getTable = function(arrTh, arrTr) {
		var s = '<table class="tbData">';
		s += '<tr>';
		for ( var i = 0; i < arrTh.length; i++) {
			s += '<th>' + arrTh[i] + '</th>';
		}
		s += '</tr>';
		for ( var i = 0; i < arrTr.length; i++) {
			s += '<tr>';
			for ( var j = 0; j < arrTr[i].length; j++) {
				s += '<td>' + arrTr[i][j] + '</td>';
			}
			s += '</tr>';
		}
		s += '</table>';
		return s;
	}

	//js分页
	//el:分页容器 count:总记录数 pageStep:每页显示多少个 pageNum:第几页 fnGo:分页跳转函数
	var jsPage = function(el, count, pageStep, pageNum, fnGo) {
		this.getLink = function(fnGo, index, pageNum, text) {
			var s = '<a href="#p' + index + '" onclick="' + fnGo + '(' + index
					+ ');" ';
			if (index == pageNum) {
				s += 'class="aCur" ';
			}
			text = text || index;
			s += '>' + text + '</a> ';
			return s;
		}

		//总页数
		var pageNumAll = Math.ceil(count / pageStep);
		if (pageNumAll == 1) {
			divPage.innerHTML = '';
			return;
		}
		var itemNum = 5; //当前页左右两边显示个数
		pageNum = Math.max(pageNum, 1);
		pageNum = Math.min(pageNum, pageNumAll);
		var s = '';
		if (pageNum > 1) {
			s += this.getLink(fnGo, pageNum - 1, pageNum, '上一页');
		} else {
			s += '<span>上一页</span> ';
		}
		var begin = 1;
		if (pageNum - itemNum > 1) {
			s += this.getLink(fnGo, 1, pageNum) + '... ';
			begin = pageNum - itemNum;
		}
		var end = Math.min(pageNumAll, begin + itemNum * 2);
		if (end == pageNumAll - 1) {
			end = pageNumAll;
		}
		for ( var i = begin; i <= end; i++) {
			s += this.getLink(fnGo, i, pageNum);
		}
		if (end < pageNumAll) {
			s += '... ' + this.getLink(fnGo, pageNumAll, pageNum);
		}
		if (pageNum < pageNumAll) {
			s += this.getLink(fnGo, pageNum + 1, pageNum, '下一页');
		} else {
			s += '<span>下一页</span> ';
		}
		var divPage = document.getElementById(el);
		divPage.innerHTML = s;
	}
</script>
</head>
<body>
	<div id="divData" style="font-family: Consolas;"></div>
	<div id="divPage"></div>
	<script type="text/javascript">
		function goPage(pageIndex) {
			$.post("AjaxAction!getAdminUser.action", {
				page : pageIndex
			}, function(data) {
				var arrTh = [ '用户账号', '用户姓名', '用户密码', '用户权限', '管理楼宇', '操作' ];
				var arrTr = [];
				$(data).each(function(i){
					if(!data[i]){
						alert('抱歉，错误，没有该页数据');
					}else if(data[i].result=="logout"){
						alert('登陆超时！');
						parent.window.location.href='login.html';
					}else if(data[i].result=="permission denied"){
						alert('抱歉，您当前登录的管理员没有该项权限！');
					}else if(data[i].result=="success"){
						var dp = "---";
						if(data[i].role==0){
							dp="A10/A17";
						}else if(data[i].role==1){
							dp="A10";
						}else if(data[i].role==2){
							dp="A17";
						}
						var dda = "<button onclick='delAdmin(\""+data[i].id+"\")'>删除</button>";
						arrTr.push([ data[i].id, data[i].name, data[i].password,
									data[i].authority, dp, dda ]);
					}
				});
				$('#divData').html(getTable(arrTh, arrTr));
				jsPage('divPage', 80, 10, pageIndex, 'goPage');
			}, "json");
		}
		function delAdmin(id){ 
			if(!confirm("确认删除账号为"+id+"的管理员？"))return false;
			$.post("AjaxAction!delAdminUser.action",{
				id : id
			},function(data){
				if(data[0].result=="logout"){
					alert('登陆超时!');
					parent.window.location.href='login.html';
				}else if(data[0].result=="permission denied"){
					alert('抱歉，您当前登录的管理员没有该项权限！');
				}else if(data[0].result=="success"){
					alert('删除成功！');
					goPage(1);
				}else if(data[0].result=="failed"){
					alert('抱歉，删除失败！');
				}
			},"json");
		}
		$(document).ready(function() {
			goPage(1);
		});
	</script>


</body>
</html>